package com.xiaofeng.pochi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaofeng.pochi.common.Page;
import com.xiaofeng.pochi.mapper.ShopCartItemMapper;
import com.xiaofeng.pochi.mapper.ShopCouponMapper;
import com.xiaofeng.pochi.mapper.ShopProductMapper;
import com.xiaofeng.pochi.pojo.*;
import com.xiaofeng.pochi.repository.ShopProductCollectionRepository;
import com.xiaofeng.pochi.service.ShopCartItemService;
import com.xiaofeng.pochi.service.ShopCouponService;
import com.xiaofeng.pochi.utils.DateUtils;
import com.xiaofeng.pochi.utils.IdWorker;
import com.xiaofeng.pochi.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/08/26/23:10
 * @Description:
 */
@Service
public class ShopCartItemServiceImpl extends ServiceImpl<ShopCartItemMapper, ShopCartItem> implements ShopCartItemService {

    @Autowired
    private ShopCartItemMapper shopCartItemMapper;

    @Autowired
    private ShopProductMapper shopProductMapper;

    @Autowired
    private ShopProductCollectionRepository shopProductCollectionRepository;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void saveShop(ShopCartItem shopCartItem) {
        // 根据用户编号和商品编号查询当前用户是否已经将该商品加入购物车
        LoginUser loginUser = ShiroUtils.getLoginUser();
        shopCartItem.setCreateBy(loginUser.getUsername());
        ShopCartItem cartItem = shopCartItemMapper.getByProductIdAndCreateBy(shopCartItem);
        if (cartItem == null){
            //添加购物车
            ShopProduct product = shopProductMapper.getInfoById(shopCartItem.getProductId());
            shopCartItem.setPrice(product.getPrice());
            if (shopCartItem.getQuantity() == null){
                shopCartItem.setQuantity(1);
            }
            shopCartItem.setProductName(product.getName());
            shopCartItem.setProductPic(product.getPic());
            shopCartItemMapper.save(shopCartItem);
        }else{
            // 修改商品数
            shopCartItemMapper.updateProductCount(cartItem.getId(),cartItem.getQuantity());
        }
    }

    @Override
    public Integer getProductCount() {
        LoginUser loginUser = ShiroUtils.getLoginUser();
        String username = loginUser.getUsername();
        return shopCartItemMapper.getProductCountByUser(username);
    }

    @Override
    public List<ShopCartItem> getByPage(Page<ShopCartItem> page) {
        String username = ShiroUtils.getLoginUser().getUsername();
        Map<String,Object> params = page.getParams();
        params.put("username",username);
        return shopCartItemMapper.getByPage(page);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        shopCartItemMapper.deleteByIds(ids);
    }

    @Override
    public void moveToCollection(List<Long> ids) {
        /// 获取创建人
        LoginUser loginUser = ShiroUtils.getLoginUser();
        String username = loginUser.getUsername();
        // 查询购物车，查到所有商品ID
        List<ShopCartItem> cartItemList = shopCartItemMapper.getByIds(ids);
        List<Long> productIds = cartItemList.stream().map(ShopCartItem::getProductId).collect(Collectors.toList());
        // 查询商品信息
        List<ShopProduct> productList = shopProductMapper.getByIds(productIds);// 查询商品收藏情况
        List<ShopProductCollection> collectionList = shopProductCollectionRepository.getByProductIdInAndCreateBy(productIds, username);
        if (CollectionUtils.isEmpty(collectionList)) {
            collectionList = new ArrayList<>(0);
        }
        // 构造商品收藏列表
        final List<ShopProductCollection> finalCollectionList = collectionList;
        List<ShopProductCollection> list = new ArrayList<>();
        productList.stream().forEach(e -> {
            ShopProductCollection collection = new ShopProductCollection();
            collection.setId(idWorker.nextId());
            collection.setProductId(e.getId());
            collection.setCreateBy(username);
            // 判断当前商品在collectionList中是否存在
            if (!finalCollectionList.contains(collection)) {
                collection.setCreateTime(DateUtils.newDateTime());
                collection.setProductPic(e.getPic());
                collection.setProductName(e.getName());
                collection.setProductBrand(e.getBrandName());
                collection.setProductPrice(e.getPrice());
                collection.setProductCategoryId(e.getCategoryId());
                list.add(collection);
            }
        });
        // 存库
        shopProductCollectionRepository.saveAll(list);
        shopCartItemMapper.deleteByIds(ids);
    }
}
